//
//  DLSportTrackViewController.m
//  DLSport
//
//  Created by DongL on 2017/4/21.
//  Copyright © 2017年 DongL. All rights reserved.
//

#import "DLSportTrackViewController.h"
#import "DLMapViewController.h"
#import "DLMapModeViewController.h"

@interface DLSportTrackViewController ()<UIPopoverPresentationControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation DLSportTrackViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setMapView];
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationDidUpdateNote) name:@"DLSportLocationDidUpdateNote" object:nil];
}

- (void)setMapView {
    _mapVc = [[DLMapViewController alloc] init];
    
    
    [self addChildViewController:_mapVc];
    
    [self.view insertSubview:_mapVc.view atIndex:0];
    
    _mapVc.view.frame = self.view.bounds;
    
    [_mapVc didMoveToParentViewController:self];
    
}

- (void)setTrack:(DLSportTrack *)track{
    
    _track = track;
    //传递轨迹模型
    self.mapVc.track = track;
}

- (void)locationDidUpdateNote {
    //设置数据
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2f", self.mapVc.totalDistance];
    int time = (int) self.mapVc.totalTime;
    self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", time / 3600, (time % 3600) / 60, time % 60];
}
- (IBAction)disMissClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)changeMapMode:(id)sender {
    //Popover 弹出窗  属于modal的一种
    //创建地图类型控制器
    DLMapModeViewController *mapModeVc = [[DLMapModeViewController alloc] initWithselectBlock:^(DLMapType maptype) {
        self.mapVc.mapType = maptype;
 }];
    //将当前地图类型传递给mapMode
    mapModeVc.mapType = self.mapVc.mapType;
    //modal展示样式  iOS会自动检测设备类型,如果是iPhone上使用popover,还是会使用默认的modal样式进行适配
    mapModeVc.modalPresentationStyle = UIModalPresentationPopover;
    
    //iPhone使用Popover的解决办法: 取消系统的默认适配
    //获取Popover对象 用于设置Popover相关的数据
    UIPopoverPresentationController *popover = mapModeVc.popoverPresentationController;
    //设置Popover的代理  取消Modal的设备适配
    popover.delegate = self;
    //设置来源视图/BarButtonItem  展示Popover之前,必须设置参考点(参考的控件)
    popover.sourceView = sender;
    //设置Popover的大小 preferredContentSize只能用于修改进行Popover展示的控制器的视图的大小
    mapModeVc.preferredContentSize = CGSizeMake(300, 120);
    //设置箭头方向
    popover.permittedArrowDirections = UIPopoverArrowDirectionDown;
    //进行展示
    [self presentViewController:mapModeVc animated:YES completion:nil];

}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//对Modal样式进行自适应时调用
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    //取消适配
    return UIModalPresentationNone;
}

@end
